﻿$(function(){
	$.fn.extend({
		loadCombox:function(on_type){
			this.find("input[on_type='"+on_type+"']").each(function(index,_this){
				var select_id=$(_this).attr("id");
				var select_value=$(_this).attr("value");
				var select_loadUrl=$(_this).attr("loadUrl");
				//$(_this).hide();
				var selectStr='<select class="col-xs-10 col-sm-5" id="'+select_id+'_selects"/>';
				$(_this).after(selectStr);
				$.post(select_loadUrl,{},function(data){
					console.log(data);
					var json=$.parseJSON(data);
					if(json.res=="1"){
						var optionStr='<option value="-1">请选择</option>';
						//<option value="-1">请选择</option>
						var comboxJson=json.obj;
						for(var i=0;i<comboxJson.length;i++){
							console.log(comboxJson);
							if(comboxJson[i].industryName!=null){
								comboxJson[i].id=comboxJson[i].id;
								comboxJson[i].name=comboxJson[i].industryName;
							}
							if(comboxJson[i].columnName!=null){
								comboxJson[i].id=comboxJson[i].id;
								comboxJson[i].name=comboxJson[i].columnName;
							}
							if(select_value==''||select_value==null){
								optionStr += '<option value="'+comboxJson[i].id+'">'+comboxJson[i].name+'</option>';
							}else{
								var selected="";
								if(select_value==comboxJson[i].id){
									selected='selected="selected"';
								}
								optionStr += '<option value="'+comboxJson[i].id+'" '+selected+'>'+comboxJson[i].name+'</option>';
							}		
						}
						$("#"+select_id+"_selects").append(optionStr);
					}
				});
				$("#"+select_id+"_selects").change(function(){
					var onVal=$(this).val();
					$(_this).attr("value",onVal);
				});
			});
		},loadFy:function(json,cl,pasrams,url){
			$("#fy").remove();
			var _table_this=this;
			_table_this.after('<div class="modal-footer no-margin-top" style="display:none" id="fy"></div>');
			$("#fy").append('<ul class="pagination pull-right no-margin"></ul>')
			var nowPageNum=json.obj.pageNum;
			var nowPageSize=json.obj.pageSize;
			var tR=json.obj.totalRecord;
			if(tR>0){
				$("#fy").show();
				var prevDisabled="";
				if(nowPageNum==1){
					prevDisabled="disabled";
				}
				var fyLi='<li class="prev '+prevDisabled+'" id="diyiye"><a href="javascript:void(0);" onclick="">';
				fyLi+='第一页</a></li>';
				fyLi+='<li class="prev '+prevDisabled+'" id="shangyiye"><a href="javascript:void(0);" onclick="">';
				fyLi+='<i class="icon-double-angle-left"></i></a></li>';
				for(var i=1;i<=json.obj.totalPage;i++){
					var acStr='';
					if(json.obj.pageNum==i){
						acStr='active';
					}
						fyLi+='<li class="dangqianye '+acStr+'" name="'+i+'"><a href="javascript:void(0);" onclick="">'+i+'</a></li>';
				}
				var nextDisabled="";
				if(nowPageNum==json.obj.totalPage){
					nextDisabled="disabled";
				}
				fyLi+='<li class="next '+nextDisabled+'" id="xiayiye"><a href="javascript:void(0);" onclick="">';
				fyLi+='<i class="icon-double-angle-right"></i></a></li>';
				fyLi+='<li class="next '+nextDisabled+'" id="zuihouye"><a href="javascript:void(0);" onclick="">';
				fyLi+='最后页</a></li>';
				$("#fy>ul").find("li").remove();
				$("#fy>ul").append(fyLi);
				$("#diyiye").click(function(){
					pasrams.pageNum=1;
					_table_this.loadTbl(cl,pasrams,url,true);
				});
				$("#shangyiye").click(function(){
					if(pasrams.pageNum>1){
						pasrams.pageNum=pasrams.pageNum-1;
						_table_this.loadTbl(cl,pasrams,url,true);
					}
				});
				$(".dangqianye").click(function(){
					var i=$(this).attr("name");
					pasrams.pageNum=i;
					_table_this.loadTbl(cl,pasrams,url,true);
				});
				$("#xiayiye").click(function(){
					if(json.obj.totalPage>pasrams.pageNum){
						pasrams.pageNum=pasrams.pageNum+1;
						_table_this.loadTbl(cl,pasrams,url,true);
					}
				});
				$("#zuihouye").click(function(){
					pasrams.pageNum=json.obj.totalPage;
					_table_this.loadTbl(cl,pasrams,url,true);
				});
			}
		},loadTbl:function(cl,pasrams,url,isFY){
			var _table_this = this;
			$.post(url,pasrams,function(data){
				var json=$.parseJSON(data);
				if(json.resMsg=="数据查询为0"){
					alert("该新闻栏目下没有新闻");
				}
				if(json.res=="1"){
					var result=json.obj.results;
					console.log(json);
					var tdStr='<tr>';
					for(var i=0;i<result.length;i++){
						for(var j=0;j<cl.length;j++){
							var clOne=cl[j].name;
							var clTwo=cl[j].cl;
							var tbl_html="";
							if(typeof clTwo=='function' && clTwo!=null){
								tbl_html=clTwo(j,result[i][""+clOne+""]);
							}
							if(tbl_html!=""){
								if(j!=0){
									tdStr+='<td>'+tbl_html+'</td>';
								}
								else{
									tdStr+='<td class="center">'+tbl_html+'</td>';
								}
							}else{
								tdStr+='<td>'+result[i][""+clOne+""]+'</td>';
							}
						}
						tdStr+='</tr>';
					}
					_table_this.children("tbody").eq(0).find("tr").remove();
					_table_this.children("tbody").eq(0).append(tdStr);
					_table_this.loadFy(json,cl,pasrams,url);
				}
			});
		},getForm:function(_from){
			var dataJson = "";
			var dataArr = [];
			$(_from).find("input").each(function(index,_this){
				var inputName = $(_this).attr("name");
				if(inputName!=""&&inputName!=null){
					dataArr.push(inputName+":'" + $(_this).val()+"'");
				};
			});
			dataJson = "{"+dataArr.join(",")+"}";
			var jsonForms=eval("("+dataJson+")");
			return jsonForms;
		},loadInfo:function(pasrams,url,jpath){
			$.post(url,pasrams,function(data){
				var json=$.parseJSON(data);
				if(json.obj==null){
					var info_html="";
					info_html+="<div class='container'>";
					info_html+="<div class='col-xs-3'></div><div class='col-xs-6' style='color: #939393;;'>没有更多的内容</div><div class='col-xs-3'></div>";
					info_html+="</div>";
					$("#content").append(info_html);
					kongzhi=0;
					return;
				}
				if(json.res=="1"){
					var result=json.obj.results;
					console.log(result);
					for(var i=0;i<result.length;i++){
						var info_html="";
						info_html+="<div class='container'>";
						info_html+="<div class='row content_c info' nid="+result[i].id+">";
						info_html+="<div class='col-xs-4 clear_padding'><img src='"+jpath+"/static/qd/img/s_img.png' style='width:100%;'/></div>";
						info_html+="<div class='col-xs-8 clear_padding left_pad'><div class='container'><div class='row'>";
						info_html+="<div class='col-xs-12 content_m_text clear_padding content_text_b_pad'>"+result[i].title+"</div>";
						if(result[i].newsContent.length>60){
							info_html+="<div class='col-xs-12 content_s_text clear_padding content_s_text_color content_text_b_pad' style='word-wrap: break-word;'>"+result[i].newsContent.substring(0,60) +'....'+"</div></div>";
						}else{
							info_html+="<div class='col-xs-12 content_s_text clear_padding content_s_text_color content_text_b_pad' style='word-wrap: break-word;'>"+result[i].newsContent+"</div></div>";
						}
						info_html+="<div class='row'>";
						info_html+="<div class='col-xs-6 content_s_text clear_padding'>"+result[i].releaseTimeToString+"</div>";
						info_html+="<div class='col-xs-6 content_s_text clear_padding align_right'>来源:"+result[i].publisher+"</div>";
						info_html+="<div class='row'>";
						info_html+="</div></div></div>";
						info_html+="</div>";
						$("#content").append(info_html);
					}
				}
			});
		}
	});
});